<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-select
          v-model="query.address"
          placeholder="地址"
          class="handle-select mr10"
        >
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select>
        <el-input
          v-model="query.name"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input>
        <!-- <el-button
          type="danger"
          class="handle-del mr10"
          @click="delAllSelection"
          >删除</el-button
        > -->
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="index"
          label="排序"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="menuType"
          label="菜单类型"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="menuName"
          label="菜单名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="fatherName"
          label="上级菜单"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="url"
          label="网页链接"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="sort"
          label="排序"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="miniprogramAppid"
          label="小程序appid"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="miniprogramPagepath"
          label="小程序页面路径"
          align="center"
        ></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="30%">
      <el-form ref="addForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="父菜单">
          <template>
            <el-select
              v-model="addForm.fatherId"
              placeholder="请选择"
              style="margin-right: 70px; margin-bottom:10px;"
              @change="selectFatherMenuId"
            >
              <el-option
                v-for="(item, index) in options1"
                :key="index"
                :label="item.menuName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="菜单名称" prop="required">
          <el-input
            v-model="addForm.menuName"
            style="max-width: 192px;width: 100%"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="菜单类型">
          <template>
            <el-select
              v-model="addForm.menuType"
              placeholder="请选择"
              style="margin-right: 70px;margin-bottom:10px;"
              @change="selectMenuType"
            >
              <el-option
                v-for="item in menuType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>

          <!-- <el-input
            class="menu-two"
            params="菜单事件"
            v-model="input"
            style="max-width: 192px;width: 100%"
            placeholder="请输入内容"
          ></el-input> -->
        </el-form-item>

        <el-form-item label="菜单链接">
          <el-input v-model="addForm.url"></el-input>
        </el-form-item>

        <el-form-item label="排序" prop="required">
          <template>
            <el-input-number
              v-model="addForm.sort"
              @change="handleChange"
              :min="1"
              :max="6"
              label="描述文字"
            ></el-input-number>
          </template>
        </el-form-item>

        <el-form-item label="小程序appid">
          <el-input
            :disabled="disabled"
            v-model="addForm.miniprogramAppid"
          ></el-input>
        </el-form-item>

        <el-form-item label="小程序页面路径">
          <el-input
            :disabled="disabled"
            v-model="addForm.miniprogramPagepath"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="editForm" :model="editForm" label-width="auto">
        <el-form-item label="父菜单">
          <template>
            <!-- <el-select
              v-model="value3"
              placeholder="请选择"
              style="margin-right: 70px; margin-bottom:10px;"
            >
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select> -->

            <el-select
              v-model="editForm.fatherId"
              placeholder="请选择"
              style="margin-right: 70px; margin-bottom:10px;"
              @change="selectFatherMenuId"
            >
              <el-option
                v-for="(item, index) in options1"
                :key="index"
                :label="item.menuName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="菜单名称">
          <el-input
            v-model="editForm.menuName"
            style="max-width: 192px;width: 100%"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="菜单类型">
          <template>
            <el-select
              v-model="editForm.menuType"
              placeholder="请选择"
              style="margin-right: 70px;margin-bottom:10px;"
            >
              <el-option
                v-for="item in menuType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>

          <!-- <el-input
            class="menu-two"
            params="菜单事件"
            v-model="input"
            style="max-width: 192px;width: 100%"
            placeholder="请输入内容"
          ></el-input> -->
        </el-form-item>

        <el-form-item label="菜单链接">
          <el-input v-model="editForm.url"></el-input>
        </el-form-item>

        <el-form-item label="排序">
          <template>
            <el-input-number
              v-model="editForm.sort"
              @change="handleChange"
              :min="1"
              :max="6"
              label="描述文字"
            ></el-input-number>
          </template>
        </el-form-item>

        <el-form-item label="小程序appid">
          <el-input
            :disabled="disabled"
            v-model="editForm.miniprogramAppid"
          ></el-input>
        </el-form-item>

        <el-form-item label="小程序页面路径">
          <el-input
            :disabled="disabled"
            v-model="editForm.miniprogramPagepath"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from "utils/util";
export default {
  name: "WechatMenuManage",
  data() {
    return {
      input: "",
      menuName: "",
      rules: {
        required: [{ required: true, message: "必填不能为空", trigger: "blur" }]
      },
      //   注意： 返回的数据中value一定不能重复, 会搞死人
      options: [
        {
          value: "选项1",
          label: "黄金糕"
        },
        {
          value: "选项2",
          label: "双皮奶"
        },
        {
          value: "选项3",
          label: "蚵仔煎"
        },
        {
          value: "选项4",
          label: "龙须面"
        },
        {
          value: "选项5",
          label: "北京烤鸭"
        }
      ],
      menuType: [
        {
          value: "view",
          label: "网页类型"
        },
        {
          value: "click",
          label: "点击类型"
        },
        {
          value: "miniprogram",
          label: "小程序类型"
        }
      ],
      value1: "",
      value2: "",
      value3: "",
      value4: "",
      options1: [{ id: "", menuName: "" }],
      //   options2: [{ id: "", menuName: "" }],

      value: "",
      num: 1,

      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      form: {},
      addForm: {
        fatherId: "",
        menuType: "",
        menuName: "",
        url: "",
        sort: "",
        miniprogramAppid: "",
        miniprogramPagepath: ""
      },
      editForm: {
        fatherId: "",
        menuType: "",

        menuName: "",
        url: "",
        sort: "",
        miniprogramAppid: "",
        miniprogramPagepath: ""
      },
      idx: -1,
      id: -1,
      disabled: true
    };
  },
  created() {
    this.getData();
  },
  watch: {
    addVisible(newVal, oldVal) {
      if (newVal == true) {
        this.addForm = {
          fatherId: "",
          menuType: "",
          menuName: "",
          url: "",
          sort: "",
          miniprogramAppid: "",
          miniprogramPagepath: ""
        };
      }
    },
    "addForm.menuType"(newVal, oldVal) {
      console.log(newVal, oldVal);
      if (newVal == "miniprogram") {
        this.disabled = false;
      } else {
        this.disabled = true;
      }
    },
    "editForm.menuType"(newVal, oldVal) {
      console.log(newVal, oldVal);
      if (newVal == "miniprogram") {
        this.disabled = false;
      } else {
        this.disabled = true;
        this.editForm.miniprogramAppid = "";
        this.editForm.miniprogramPagepath = "";
      }
    }
  },
  methods: {
    selectFatherMenuId(fatherId) {
      console.log(fatherId);
      this.addForm.fatherId = fatherId;
    },
    selectMenuType(menuType) {
      console.log(menuType);
      this.addForm.menuType = menuType;
    },

    handleChange(value) {
      console.log(value);
    },
    // 获取 easy-mock 的模拟数据
    getData() {
      this.$axios
        .get("/wx/menu/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize
          }
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
          this.pageTotal = res.data.total;
        })
        .catch(err => {});
    },
    handleAdd() {
      this.addVisible = true;
      this.getFatherMenuId();
    },
    getFatherMenuId() {
      return this.$axios.get("/wx/menu/getFatherMenu").then(({ data: res }) => {
        console.log(res);
        if (res.code !== 0) {
          return this.$message.error({
            message: res.msg,
            duration: 1500
          });
        }
        this.options1 = res.data;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      //   this.ids = [];
      //   var ids = this.ids;
      //   ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.delete(row.id);
        })
        .catch(() => {});
    },
    delete(id) {
      this.$axios
        .delete("/wx/menu", {
          data: { id }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error({
              duration: 1500,
              message: res.msg
            });
          }
          this.$message.success("删除成功");
          this.getData();
        })
        .catch(() => {});
    },
    // 多选操作
    // handleSelectionChange(val) {
    //   this.multipleSelection = val;
    // },
    // delAllSelection() {
    //   const length = this.multipleSelection.length;
    //   let str = "";
    //   this.delList = this.delList.concat(this.multipleSelection);
    //   for (let i = 0; i < length; i++) {
    //     str += this.multipleSelection[i].name + " ";
    //   }
    //   this.$message.error(`删除了${str}`);
    //   this.multipleSelection = [];
    // },
    // 编辑操作
    handleEdit(index, row) {
      console.log(index, row);
      //   this.idx = index;
      //   this.form = row;
      this.getEdit(row.id);
      this.editVisible = true;
    },
    getEdit(id) {
      this.getFatherMenuId().then(() => {
        this.$axios
          .get(`/wx/menu/${id}`)
          .then(({ data: res }) => {
            console.log(res);
            if (res.code !== 0) {
              return this.$message.error({
                duration: 1500,
                message: res.msg
              });
            }
            // this.value3 = res.data.fatherId;
            this.editForm = {
              fatherId: "",
              menuType: "",

              menuName: "",
              url: "",
              sort: "",
              miniprogramAppid: "",
              miniprogramPagepath: ""
            };
            this.editForm.id = res.data.id;
            this.editForm.fatherId = res.data.fatherId;
            this.editForm.menuName = res.data.menuName;
            this.editForm.menuType = res.data.menuType;
            this.editForm.url = res.data.url;
            this.editForm.sort = res.data.sort;

            if (this.editForm.menuType == "miniprogram") {
              this.disabled = false;
              this.editForm.miniprogramAppid = res.data.miniprogramAppid;
              this.editForm.miniprogramPagepath = res.data.miniprogramPagepath;
            }
          })
          .catch(err => {});
      });
    },
    // 保存新增
    saveAdd() {
      var bool = util.isEmpty({
        menuName: this.addForm.menuName,
        sort: this.addForm.sort
      });
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }
      this.$axios
        .post("/wx/menu", {
          ...this.addForm
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code !== 0) {
            return this.$message.error({
              duration: 1500,
              message: res.msg
            });
          }
          this.$message.success({
            duration: 1500,
            message: "新增成功"
          });

          (this.addForm = {
            fatherId: "",
            menuType: "",

            menuName: "",
            url: "",
            sort: "",
            miniprogramAppid: "",
            miniprogramPagepath: ""
          }),
            this.getData();
          this.addVisible = false;
        })
        .catch(err => {});
    },
    // 保存编辑
    saveEdit() {
      var bool = util.isEmpty({
        id: this.editForm.id,
        menuName: this.editForm.menuName,
        sort: this.editForm.sort
      });
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }
      this.$axios.put("/wx/menu", this.editForm).then(({ data: res }) => {
        if (res.code != 0) {
          return this.$message.error({
            duration: 1500,
            message: res.msg
          });
        }
        this.$message.success("操作成功");
        this.editVisible = false;
        this.getData();
      });
      //   this.editVisible = false;
      //   this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      //   this.$set(this.tableData, this.idx, this.form);
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
/* .menu-one {
  position: relative;
}
.menu-one::before {
  content: "菜单名";
  text-align: right;
  display: inline-block;
  position: absolute;
  width: 120px;
  height: auto;
  left: -132px;
} */

.menu-two {
  position: relative;
}
.menu-two::before {
  content: "菜单事件";
  text-align: right;
  display: inline-block;
  position: absolute;
  width: 120px;
  height: auto;
  left: -132px;
}
</style>
